package LanQiao.jichu;

import java.util.Scanner;

//取回型数
public class BASIC_25 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = input.nextInt();
            }
        }
        int count = 0;
        //x从-1开始是因为这里用的是先加再运算，如果不从-1开始则第一个数会在最后输出
        int x = -1, y = 0;
        while (count < n * m) {
            //while中前边的加1和减1是防止在下一个while循环时下标越界，后边的侧是提前预判下一个位置是否被引用过，如果引用过则停止加或减操作
            while (x + 1 < n && arr[x+1][y] != -1) {
                System.out.print(arr[++x][y] + " ");
                arr[x][y] = -1;
                count++;
            }
            while (y + 1 < m && arr[x][y+1] != -1) {
                System.out.print(arr[x][++y] + " ");
                arr[x][y] = -1;
                count++;
            }
            while (x - 1 >= 0 && arr[x-1][y] != -1) {
                System.out.print(arr[--x][y] + " ");
                arr[x][y] = -1;
                count++;
            }
            while (y - 1 >= 0 && arr[x][y-1] != -1) {
                System.out.print(arr[x][--y] + " ");
                arr[x][y] = -1;
                count++;
            }
        }
    }
}
